//
//  ZGLLoginZCView.m
//  Faker
//
//  Created by dllo on 15/11/10.
//  Copyright © 2015年 dllo. All rights reserved.
//
#import <AVOSCloud/AVOSCloud.h>
#import "ZGLLoginZCView.h"
@interface ZGLLoginZCView()<UITextFieldDelegate>
@property(nonatomic, retain)UITextField *name;
@property(nonatomic, retain)UITextField *pwd;
@property(nonatomic, retain)UITextField *pwd2;
@end
@implementation ZGLLoginZCView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor greenColor];
        
        UILabel *nameLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 80, 40)];
        nameLab.text = @"用户名: ";
        nameLab.textAlignment = NSTextAlignmentRight;
        [self addSubview:nameLab];
        
        UILabel *pwdLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 80, 80, 40)];
        pwdLab.text = @"密码: ";
        pwdLab.textAlignment = NSTextAlignmentRight;
        [self addSubview:pwdLab];
        
        UILabel *pwdLab2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 140, 80, 40)];
        pwdLab2.text = @"确认密码: ";
        pwdLab2.textAlignment = NSTextAlignmentRight;
        [self addSubview:pwdLab2];
        
        _name = [[UITextField alloc]initWithFrame:CGRectMake(100*ScaleX, 20, 180, 40)];
        _name.placeholder = @"请输入用户名";
        _name.borderStyle = 3;
        _name.backgroundColor = [UIColor lightGrayColor];
        _name.keyboardType = UIKeyboardTypeDefault;
        [_name becomeFirstResponder];
        _name.delegate = self;
        _name.tag = 3502;
        [self addSubview:_name];
        
        _pwd = [[UITextField alloc]initWithFrame:CGRectMake(100*ScaleX, 80, 180, 40)];
        _pwd.placeholder = @"请输入密码";
        _pwd.borderStyle = 3;
        _pwd.backgroundColor = [UIColor lightGrayColor];
        _pwd.secureTextEntry = YES;//安全输入
        _pwd.keyboardType = UIKeyboardTypeASCIICapable;
        _pwd.delegate = self;
        _pwd.tag = 3503;
        [self addSubview:_pwd];
        
        _pwd2 = [[UITextField alloc]initWithFrame:CGRectMake(100*ScaleX, 140, 180, 40)];
        _pwd2.placeholder = @"请再次确认密码";
        _pwd2.borderStyle = 3;
        _pwd2.backgroundColor = [UIColor lightGrayColor];
        _pwd2.secureTextEntry = YES;//安全输入
        _pwd2.keyboardType = UIKeyboardTypeASCIICapable;
        _pwd2.delegate = self;
        _pwd.tag = 3504;
        [self addSubview:_pwd2];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(120*ScaleX, 200, 40, 20);
        [button setTitle:@"注册" forState:UIControlStateNormal];
        button.selected = NO;
        [self addSubview:button];
        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)click:(UIButton *)button{
    if (!([_pwd.text isEqualToString:_pwd2.text])) {
        [self arLog:@"密码不一样"];
    }else if([_name.text isEqualToString:@""]||[_pwd.text isEqualToString:@""]) {
        [self arLog:@"不能为空"];
    }else{
        if (!button.selected) {
            button.selected = YES;
            AVUser *user= [AVUser user];
            user.username=_name.text;
            user.password=_pwd.text;
            [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    [self arLog:@"用户注册成功"];
                    //注册成功后也可以直接通过currentUser获取当前登陆用户
                    //[self arLog:[NSString stringWithFormat:@"当前用户 %@",[[AVUser currentUser] username]]];
                }else{
                    [self arLog:[NSString stringWithFormat:@"错误:用户名重复 %@",[error description]]];
                }
                button.selected = NO;
            }];
        }
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    switch (textField.tag) {
        case 3502:
            [_pwd becomeFirstResponder];
            break;
        case 3503:
            [_pwd2 becomeFirstResponder];
            break;
        default:
            [textField resignFirstResponder];
            break;
    }
    return YES;
}

- (void)arLog:(NSString *)str{
    alartLog(str);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
